﻿// ┌────────────────────────────────────────────────
// │  control class :: requirement
// │  1 :: rms.core.js
// │  2 :: 
// │  3 :: 
// │  4 :: 
// │  5 ::
// └────────────────────────────────────────────────
//=====================================================================================================
// Class Base Control Contructor
//=====================================================================================================
(function (RMS) {
    function BaseEntry(settings) {
        this.init(settings);
    };
    //======================================================
    BaseEntry.prototype.extend = function () {
        var baseClass = this.constructor;
        function Entry() {
            baseClass.apply(this, arguments);
        }

        function inherit(Child, Parent) {
            function Bridge() { };

            Bridge.prototype = Parent.prototype;
            Child.prototype = new Bridge();
            Child.prototype.constructor = Child;
            return Child;
        };

        return inherit(Entry, baseClass);
    };

    BaseEntry.prototype.init = function () {
        throw Error('BaseControl init() method unimplemented');
    };

    BaseEntry.prototype.binding = function () {
        throw Error('BaseControl binding() method unimplemented');
    };

    BaseEntry.prototype.load = function () {
        throw Error('BaseControl load() method unimplemented');
    };

    BaseEntry.prototype.getInstance = function () {
        throw Error('BaseControl getInstance() method unimplemented');
    };

    BaseEntry.prototype.updateSettings = function () {
        throw Error('BaseControl updateSettings() method unimplemented');
    };

    BaseEntry.prototype.getSettings = function () {
        throw Error('BaseControl getSettings() method unimplemented');
    };

    BaseEntry.prototype.isCompleted = false;

    BaseEntry.prototype.loadComplete = function () {
        this.isCompleted = true;
        throw Error('BaseEntry loadComplete() method unimplemented');
    };

    BaseEntry.prototype.showInfo = function () {
        throw Error('BaseEntry showInfo() method unimplemented');
    };

    //======================================================
    //something here: enable/disable,...
    RMS.BaseEntry = BaseEntry;
})(RMS);
//=====================================================================================================
//  Entry Constructor Class
//=====================================================================================================
(function (RMS) {
    //public properties
    var Entry = RMS.BaseEntry.prototype.extend(),
        defaults = {},
        controls,
        requests,
        $entry;
    //======================================================
    Entry.prototype.isCompleted = false;
    //start public function
    Entry.prototype.init = function (settings) {
        if (settings) {
            defaults = $.extend(true, defaults, settings);
        }
        var _this = this;
        //define
        $entry = $(defaults.id);
        controls = {};
        requests = [];
        //init meta data control
        initMeta();
        //init request data source
        initRequest(function(data){
            //init control
            for (var i in controls) {
                //init with data source
                if (controls[i].requestIdx) {
                    controls[i].settings.source = data[controls[i].requestIdx];
                }
                controls[i].$Control = new RMS.CControl(controls[i].settings);
            }
            _this.loadComplete(data);
        });            
    };

    Entry.prototype.loadComplete = function (data) {        
        //console.log('entry::loadComplete::', data);
        this.isCompleted = true;                   
        if (defaults.callback) {
            defaults.callback(data);
        }
    };

    Entry.prototype.showInfo = function (info) {

    };

    Entry.prototype.binding = function () {
        console.log('::binding');
    };

    Entry.prototype.getSettings = function () {
        console.log('::getSettings');
        return defaults;
    };

    Entry.prototype.load = function () {
        console.log('::load');
    };

    Entry.prototype.updateSettings = function (settings) {
        if (settings) {
            defaults = $.extend(true, defaults, settings);
            this.load();
        }
    };
    //end public function
    //======================================================
    //start private function
    function initMeta() {
        var val, meta;
        if ($entry.attr(RMS.consts.meta)) {
            getMeta($entry[0]);
        }
        $entry.find('[' + RMS.consts.meta + ']').each(function (idx, el) {
            getMeta(this);
        });

        function getMeta(instance) {
            val = instance.attributes[RMS.consts.meta].value;
            try {
                meta = JSON.parse(val);
            } catch (e) {
                meta = RMS.pub.parseJson(val);
            }
            if (meta) {
                if (meta.type) {
                    meta.controlType = meta.type;
                    delete meta.type;
                }
                meta.id = instance.id;
                controls[instance.id] = {
                    settings: meta
                };
            }
            //clear
            instance.attributes.removeNamedItem(RMS.consts.meta);
        };
    };

    function initRequest(callback) {
        var val, tempIdx;
        $entry.find('[' + RMS.consts.request + ']').each(function (idx, el) {
            val = this.attributes[RMS.consts.request].value;
            if (val) {
                tempIdx = $.inArray(val, requests);
                if (tempIdx == -1) {
                    requests.push(val);
                    tempIdx = idx;
                }
                if (typeof controls[this.id] != 'undefined') {
                    controls[this.id].requestIdx = tempIdx + 1;
                }
            }
            //clear
            this.attributes.removeNamedItem(RMS.consts.request);
        });

        //get request data
        var data = [];
        data.push([{ Description: 'Successfully'}]);
        data.push([{ ID: '1', Name: 'A', DefaultValue: false, Group: 'PMSA' }, { ID: '2', Name: 'B', DefaultValue: true, Group: 'SBB' }, { ID: '3', Name: 'C', DefaultValue: true, Group: 'HSBC' }, { ID: '4', Name: 'D', DefaultValue: false, Group: 'VGB'}]);
        data.push([{ ID: '22', Name: 'AA' }, { ID: '33', Name: 'BB'}]);

        if (callback) {
            callback(data);
        }        
    };

    function initControl() {
        controls[id] = new RMS.CControl({ controlType: meta.type, id: id });
    };
    //end private function
    //======================================================    
    RMS.CEntry = Entry;
})(RMS);
